﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace WpfApplication1
{
    public class User : Userslib.User, IEquatable<String>
    {
        new public String Username { get { return this.username; } set { this.username = value; } }
        private StringBuilder channel = new StringBuilder();
        public String Channel { get { return this.channel.ToString(); } }

        public User(String Username, params String[] Channel) : base (Username)
        {
            this.username = Username;
            
            var x = from channel in Channel where !channel.Equals(from existing in this.channel.ToString().Split(' ') select existing) select channel + " " ;
            if (x.Count() != 0)
            foreach (String channel in x)
            {
                
                this.channel.Append(channel.ToLower());
            }
            this.channel.Remove(this.channel.ToString().LastIndexOf(' '),1);
        }

        public bool addChannel(String Channel)
        {
            var x = from chan in channel.ToString().Split(' ') where chan.Equals(Channel.ToLower()) select chan;
            if (x.Count() == 0)
                this.channel.Append(" " + Channel.ToLower());
            else
                return false;

            return true;
        }

        public bool removeChannel(String Channel)
        {
            var x = from chan in channel.ToString().Split(' ') where chan.Equals(Channel.ToLower()) select chan;
            if (x.Count() > 0)
            {

                this.channel.Remove(this.channel.ToString().IndexOf(x.FirstOrDefault().ToString().ToLower()), x.FirstOrDefault().Length);
                Channel = this.channel.ToString();
                this.channel.Clear();
                this.channel.Append(Channel.Trim());
            }
            else
                return false;

            return true;
        
        }
        public bool isInChannel(String Channel)
        {
            return channel.ToString().Split(' ').Contains<String>(Channel);
        }

        public bool isOnlyChannel(String Channel)
        {
            return channel.ToString().Trim().Equals(Channel.Trim().ToLower());
        }
        public override bool Equals(object obj)
        {
            if (obj.GetType().Equals(typeof(String)))
                return (this.username.Equals(obj.ToString()));
            var x = obj as User;
            if (x != null)
                return x.username.Equals(x.username);
            return false;
                
        }

        public bool Equals(User Instance)
        {
            if (Instance == null)
                return false;

            return Instance.username.Equals(this.username);
        }

        public bool Equals(String Name)
        {
            if (Name == null)
                return false;

            return this.username.Equals(Name);
        }

    }

}
